package com.gt.andr.webbrowser;

import java.io.File;

import javax.xml.transform.TransformerException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class UserDisplayActivity extends Activity {
	WebView userView;
	final String sdPath = Environment.getExternalStorageDirectory()
			.getAbsolutePath();
	final File xmlFile = new File(sdPath + "/usera.xml");
	final File xsltFile = new File(sdPath + "/usera.xsl");

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.userwebview);
		initComponents();
		loadTransformedHtml();
	}

	private void loadTransformedHtml() {
		try {
			String htmlTransformed = XmlTransform.getTransformedHtml(xmlFile,xsltFile);
			userView.loadData(htmlTransformed, "text/html", "utf-8");
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	}


	private void initComponents() {
		userView = (WebView) findViewById(R.id.userwebview);
		/* Register a new JavaScript interface called HTMLOUT */  
		userView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");  
		  
		/* WebViewClient must be set BEFORE calling loadUrl! */  
		userView.setWebViewClient(new WebViewClient() {  
		    @Override  
		    public void onPageFinished(WebView view, String url)  
		    {  
		        /* This call inject JavaScript into the page which just finished loading. */
		    	//sets required part of document into HTMLOUT object in DOM
		    	userView.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
		    	
		    }  
		});  
		  
		}
	class MyJavaScriptInterface
	{
	    public void processHTML(String html)
	    {
	       System.out.println(html);
	    }
	}

}
